package com.imu.purse.modules.trade.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.imu.purse.modules.trade.entity.ScheduleLogEntity;
import com.imu.core.base.BaseMapper;

/**
 * 
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-04-03 09:26:57
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
public interface ScheduleLogMapper extends BaseMapper<ScheduleLogEntity>
{
	/**
     * 分页查询
     * @param pageNo 页号
     * @param pageSize 每页显示记录数
     * @return
     */
	@Select({
		"<script>",
		    "SELECT id,",
					"schedule_name, ",
					"status, ",
					"start_height, ",
					"end_height, ",
					"time_create, ",
					"time_update, ",
					"start_time, ",
					"end_time",
		    "FROM imu_schedule_log ",
		    "WHERE status != -1",
			"<when test = 'scheduleName != null'>",
		    	"AND `schedule_name` = #{scheduleName}",
		    "</when>",
			"<when test = 'status != null'>",
		    	"AND `status` = #{status}",
		    "</when>",
			"<when test = 'startHeight != null'>",
		    	"AND `start_height` = #{startHeight}",
		    "</when>",
			"<when test = 'endHeight != null'>",
		    	"AND `end_height` = #{endHeight}",
		    "</when>",
			"<when test = 'timeCreate != null'>",
		    	"AND `time_create` = #{timeCreate}",
		    "</when>",
			"<when test = 'timeUpdate != null'>",
		    	"AND `time_update` = #{timeUpdate}",
		    "</when>",
			"<when test = 'startTime != null'>",
		    	"AND `start_time` = #{startTime}",
		    "</when>",
			"<when test = 'endTime != null'>",
		    	"AND `end_time` = #{endTime}",
		    "</when>",
		    "<when test = 'stimeStr != null'>",
		    	"AND cs &gt;= #{stimeStr}",
		    "</when>",
		    "<when test = 'etimeStr != null'>",
				"AND cs &lt;= #{etimeStr}",
			"</when>",
		    "ORDER BY cs DESC",
    	"</script>"
	})
    public List<ScheduleLogEntity> findByPage(final ScheduleLogEntity entity);	

	/**
	 * 根据任务名称和状态获取数据
	 * @param name
	 * @param status
	 * @return
	 */
	@Select({
		"<script>",
		    "SELECT max(END_HEIGHT) ",
		    "FROM imu_schedule_log ",
		    "WHERE schedule_name = #{name} and status = #{status}",
    	"</script>"
	})
	public Integer findMaxByNameAndType(@Param("name")final String name,
			@Param("status")final Integer status);
	
	/**
	 * 删除该ID成功的记录
	 * @param id
	 * @return
	 */
	@Delete({
		"delete from imu_schedule_log where id != #{id} and status = #{status}"
	})
	public int delByNotId(@Param("id") final Long id,@Param("status") Integer status);
}
